//
//  TipTool.m
//  Tip
//
//  Created by 黄星 on 15/12/4.
//  Copyright © 2015年 qingfanqie. All rights reserved.
//

#import "TipTool.h"
#import <QuartzCore/QuartzCore.h>


static CGFloat kWaitAnimationDuration = 1.5;
static CGFloat kForwardAnimationDuration = 0.3;
static CGFloat kBackwardAnimationDuration = 0.2;

@interface TipTool ()

@property (nonatomic, strong) TipLabel *labelTip;

@property (nonatomic, strong) UIWindow *window;

@end

@implementation TipTool


+ (void)showTip:(NSString *)tip
{
    [[self shareInstance] tip:tip];
}

+ (TipTool *)shareInstance
{
    static TipTool *tipTool;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        tipTool = [[TipTool alloc] init];
    });
    return tipTool;
}

- (void)tip:(NSString *)tip
{
    self.labelTip.text = tip;
    [self.labelTip sizeToFit];
    [self.labelTip.layer removeAnimationForKey:@"animation"];
    
    __block BOOL flag = NO;
    
    [self.window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj isKindOfClass:[TipLabel class]]) {
            flag = YES;
        }
        
    }];
    if (!flag) {
        [self.window addSubview:self.labelTip];
        self.labelTip.center = self.window.center;
        
        CGRect rect = self.labelTip.frame;
        rect.origin.y = [UIScreen mainScreen].bounds.size.height - 100;
        self.labelTip.frame = rect;
    }
    [self.labelTip addAnimationGroup];
}

- (UILabel *)labelTip
{
    if (!_labelTip) {
        _labelTip = [[TipLabel alloc] init];
    }
    return _labelTip;
}

- (UIWindow *)window
{
    if (!_window) {
        _window = [UIApplication sharedApplication].keyWindow;
    }
    return _window;
}

@end

typedef NS_ENUM(NSInteger, TipShowType) {
    Top = 0,
    Center = 1,
    Bottom = 2,
};

@interface TipLabel ()
{
    CFTimeInterval _forwardAnimationDuration;
    CFTimeInterval _backwardAnimationDuration;
    CFTimeInterval _waitAnimationDuration;
    UIEdgeInsets   _textInsets;
    CGFloat        _maxWidth;
}

@end

@implementation TipLabel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        _forwardAnimationDuration = kForwardAnimationDuration;
        _backwardAnimationDuration = kBackwardAnimationDuration;
        _waitAnimationDuration = kWaitAnimationDuration;
            
        _textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        _maxWidth = [UIScreen mainScreen].bounds.size.width - 20.0;
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = true;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        self.numberOfLines = 0;
        self.textAlignment = NSTextAlignmentLeft;
        self.textColor = [UIColor whiteColor];
        self.font = [UIFont systemFontOfSize:14];
    }
    return self;
}

- (void)addAnimationGroup
{
    CABasicAnimation *forwardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    forwardAnimation.duration = _forwardAnimationDuration;
    CAMediaTimingFunction *m = [[CAMediaTimingFunction alloc] initWithControlPoints:0.5 :1.7 :0.6 :0.85];
    forwardAnimation.timingFunction = m;
    forwardAnimation.fromValue = [NSNumber numberWithInt:0];
    forwardAnimation.toValue = [NSNumber numberWithInt:1];
    
    CABasicAnimation *backWardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    backWardAnimation.duration = _backwardAnimationDuration;
    backWardAnimation.beginTime = forwardAnimation.duration + _waitAnimationDuration;
    CAMediaTimingFunction *m2 = [[CAMediaTimingFunction alloc] initWithControlPoints:0.4 :0.15 :0.5 :-0.7];
    backWardAnimation.timingFunction = m2;
    backWardAnimation.fromValue = [NSNumber numberWithInt:1];
    backWardAnimation.toValue = [NSNumber numberWithInt:0];
    
    CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc] init];
    animationGroup.animations = @[forwardAnimation,backWardAnimation];
    animationGroup.duration = forwardAnimation.duration + backWardAnimation.duration + _waitAnimationDuration;
    animationGroup.removedOnCompletion = false;
    animationGroup.delegate = self;
    animationGroup.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:animationGroup forKey:@"animation"];
    
}

- (void)sizeToFit
{
    [super sizeToFit];
    CGRect rect = self.frame;
    CGFloat width = CGRectGetWidth(self.bounds) + _textInsets.left + _textInsets.right;
    rect.size.width = width > _maxWidth ? _maxWidth:width;
    rect.size.height = CGRectGetHeight(self.bounds) + _textInsets.top + _textInsets.bottom;
    rect.origin.x = [UIScreen mainScreen].bounds.size.width / 2 - rect.size.width / 2;
    self.frame = rect;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (flag) {
        [self removeFromSuperview];
    }
}

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    CGRect rect = bounds;
    
    if (self.text) {
        NSString *txt = self.text;
        rect.size = [txt boundingRectWithSize:CGSizeMake(_maxWidth - _textInsets.left - _textInsets.right, CGRectGetMaxY(self.frame)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    }
    return rect;
}

@end








